<template>
  <div class="tabs-main">
    <a-tabs v-model="tabValue" :tabBarStyle="{ backgroundColor: '#fff',marginBottom: '6px',padding: '0px 8px' }" v-if="tabsItems && tabsItems.length>0" @change="changeTabs">
      <a-tab-pane v-for="item in tabsItems" :tab="item.title" :key="item.key" />
    </a-tabs>
    <a-row type="flex" style="flex-flow: inherit;" v-if="reloading">
      <a-col :flex="toggle?'260px':'30px'" style="margin-right: 12px;background-color: #fff;">
        <depart-tree :rootOpened="true" :multi="false" @ok="getTreeData" @toggleBool="toggleBool" />
      </a-col>
      <a-col flex="auto" style="width:0;">
        <a-card :bordered="false" style="margin-bottom: 10px">
          <!-- 查询区域 -->
          <div class="table-page-search-wrapper">
            <a-form-model layout="inline" ref="ruleForm" :model="queryParam">
              <z-form-item
                :datas="fieldArr"
                :form="queryParam"
                :gutter="8"
                :submitAction="{show: true, toggle: true, span: 6}"
                @change="getChangeTime"
                @onSubmit="onQuery"
                @onReset="onResetForm"
              />
            </a-form-model>
          </div>
        </a-card>
        <a-card :bordered="false">
          <!-- 操作按钮区域 -->
          <div class="table-operator">
            <div>
              <a-button @click="handleExport">导出</a-button>
            </div>
          </div>
          <!-- 表格展示 -->
          <div class="table-page-content">
            <z-table
              ref="table"
              rowKey="sjskPersonId"
              :showAlert="false"
              :showSelection="true"
              :loading="loading"
              :columns="columns"
              :dataSource="pageData"
              @selectedRow="onSelectedRow"
              @change="onTableChange"
            />
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import * as personServiceApi from '@/api/sjsk/personService'
import departTree from '../../modules/departTree'
import { downLoadFileReader } from '@/utils/export-util/util'

export default {
  components: { departTree },
  data() {
    return {
      tabValue: 1,
      tabsItems: [
        { key: 1, title: '转入', },
        { key: 2, title: '转出', },
      ],
      loading: false,
      isDisabled: true,
      reloading: true,
      toggle: true,
      selectKeys: [],
      queryParam: {},
      fieldArr: [
        {label: '人员姓名', prop: 'name', tagType: 'input', span: 8},
        {label: '身份证号', prop: 'identityCard', tagType: 'input', span: 8},
        {label: '类型', prop: 'sjskType', tagType: 'dict-select', dictCode:'sjsk_type', span: 8, toggle: false},
        {label: '执行情况', prop: 'personStatus', tagType: 'dict-select', dictCode:'person_status', span: 8, toggle: false},
        {label: '执行时间', prop: 'date', tagType: 'range-picker',format: 'YYYY-MM-DD', span: 8, toggle: false},
      ],
      columns: [
        {
          title: '姓名',
          dataIndex: 'realName',
          width: 90,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={text} length={14} />
          }
        },
        {
          title: '身份证号',
          dataIndex: 'identityCard',
          width: 90,
          align: 'left',
          customRender: text => {
            return <j-ellipsis value={text} length={20} />
          }
        },
        {
          title: '类型',
          dataIndex: 'sjskType',
          width: 90,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={this.$filterDictTextByCache('sjsk_type', text)} length={14} />
          }
        },
        {
          title: '联系电话',
          dataIndex: 'phone',
          width: 88,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={text} length={14} />
          }
        },
        {
          title: '执行情况',
          dataIndex: 'personStatus',
          width: 90,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={this.$filterDictTextByCache('person_status', text)} length={14} />
          }
        },
        {
          title: '执行时间',
          dataIndex: 'date',
          width: 190,
          align: 'center',
          customRender: (text, record, index) => {
            return record.sjskStartDate && record.sjskEndDate? (record.sjskStartDate + '~' + record.sjskEndDate): ''
          }
        },
        {
          title: '变更前执行地',
          dataIndex: 'originalExecutionArea',
          width: 100,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={text} length={14} />
          }
        },
        {
          title: '申请变更执行地',
          dataIndex: 'nowExecutionArea',
          width: 100,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={text} length={14} />
          }
        },
        {
          title: '申请时间',
          dataIndex: 'applyTime',
          width: 110,
          align: 'center'
        },
        {
          title: '审批完成时间',
          dataIndex: 'createTime',
          width: 110,
          align: 'center'
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <span class="action-button">
                <a-button type="link" onClick={event => this.handleRouter(record)}>查看</a-button>
              </span>
            )
          }
        }
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10
      },
      pageData: {}
    }
  },
  created() {},
  mounted() {
    this.onQuery()
  },
  methods: {
    changeTabs() {
      this.reload()
      this.onResetForm()
    },
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.queryParam = {}
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.refreshTable()
    },
    onSelectedRow(keys) {
      this.selectKeys = keys
      this.isDisabled = keys.length > 0 ? false : true
    },
    onTableChange(pagination, pageFilter, sorter) {
      this.pageFilter = Object.assign({}, this.pageFilter, pagination)
      this.refreshTable()
    },
    refreshTable() {
      this.$set(this.queryParam, 'inOutType', this.tabValue)
      let {date, ...dataParam} = this.queryParam // 剔除date元素，dataParam最新的对象
      let queryParam = Object.assign({}, dataParam, this.pageFilter)
      this.loading = true
      personServiceApi
        .getChangePlaceList(queryParam)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.pageData = res.result ? res.result : {}
            this.$refs.table.onClearRowKeys()
          } else {
            this.$message.warn(res.message?res.message: '请求失败！')
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    // 导出
    handleExport(record) {
      let that = this
      let selectList = this.$refs.table.getSelectedRows()
      let idList1 = selectList.map(v => v.sjskPersonId).filter(obj => obj !== null && obj !== undefined && obj !== '')
      let idList2 = record && Object.keys(record).length > 0 && record.sjskPersonId ? [record.sjskPersonId] : []
      let params = Object.assign(
        {},
        {
          idList: selectList.length > 0 ? idList1 : idList2,
          queryChangePlaceVO: this.queryParam
        }
      )
      this.$confirm({
        title: `是否确认导出？`,
        okText: '确定',
        onOk() {
          personServiceApi.changePlaceExport(params).then(async res => {
            const fileName = '社戒社康人员-变更执行地列表数据.xlsx'
            await downLoadFileReader(res, fileName)
            that.$refs.table.onClearRowKeys()
          })
        }
      })
    },
    // 弹窗成功接收
    modalOk(type) {
      this.onQuery()
    },
    handleRouter(record) {
      let id = record instanceof Object?record.applyId: record
      let path = id?`/examine/details/1/${id}`:``
      this.$router.push({
        path: path,
        query: {},
      })
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = false
      this.$nextTick(() => (this.reloading = true))
    },
    getTreeData(datas, id) {
      console.log(datas, id)
      console.log(id)
    },
    toggleBool(bool) {
      console.log(bool)
      this.toggle = bool
    },
    // 获取组件的时间， prop为对应的字段，date为选中的时间值
    getChangeTime(prop, date) {
      if(prop === 'date') {
        this.$set( this.queryParam, 'startDate', date[0] )
        this.$set( this.queryParam, 'endDate', date[1] )
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
